/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-03
*/
package com.rzico.basics.controller;

import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Designer;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.WxPage;
import com.rzico.basics.service.DesignerService;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.WxPageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设计器控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-03
 */
@Api(description = "设计器接口")
@RestController
@RequestMapping("/designer")
public class DesignerController extends BaseController {

    @Autowired
    private DesignerService designerService;

    @Autowired
    private WxPageService wxPageService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 查询组件列表
     *
     * @return
    */
    @ApiOperation("查询组件列表")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "mchId", value = "商户号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageId", value = "页面id（不传代表首页）", dataType = "String", paramType = "query")
    })
    public CommResult<Designer> list(String mchId,Long pageId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sortField", "orders");
        params.put("sortType", "ASC");
        Enterprise enterprise = enterpriseService.findByMch(mchId);
        params.put("enterpriseId",enterprise.getId());
        if (pageId==null) {
            WxPage wxPage = wxPageService.getHome(enterprise);
            if (wxPage==null) {
                //兼容历史
                pageId = 0L;
            } else {
                pageId = wxPage.getId();
            }
        }
        params.put("pageId",pageId);
        List<Designer> list = designerService.selectList(params);
        return CommResult.success(list);
    }

}
